package cn.iocoder.yudao.module.erp.service.business.tax.taxbill;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxbill.vo.TaxBillPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxbill.vo.TaxBillSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.tax.taxbill.TaxBillDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.tax.taxbill.TaxBillMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.TAX_BILL_NOT_EXISTS;

/**
 * 发票类型 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class TaxBillServiceImpl implements TaxBillService {

    @Resource
    private TaxBillMapper taxBillMapper;

    @Override
    public Long createTaxBill(TaxBillSaveReqVO createReqVO) {
        // 插入
        TaxBillDO taxBill = BeanUtils.toBean(createReqVO, TaxBillDO.class);
        taxBillMapper.insert(taxBill);
        // 返回
        return taxBill.getId();
    }

    @Override
    public void updateTaxBill(TaxBillSaveReqVO updateReqVO) {
        // 校验存在
        validateTaxBillExists(updateReqVO.getId());
        // 更新
        TaxBillDO updateObj = BeanUtils.toBean(updateReqVO, TaxBillDO.class);
        taxBillMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaxBill(Long id) {
        // 校验存在
        validateTaxBillExists(id);
        // 删除
        taxBillMapper.deleteById(id);
    }

    private void validateTaxBillExists(Long id) {
        if (taxBillMapper.selectById(id) == null) {
            throw exception(TAX_BILL_NOT_EXISTS);
        }
    }

    @Override
    public TaxBillDO getTaxBill(Long id) {
        return taxBillMapper.selectById(id);
    }

    @Override
    public PageResult<TaxBillDO> getTaxBillPage(TaxBillPageReqVO pageReqVO) {
        return taxBillMapper.selectPage(pageReqVO);
    }

}